# -*- coding: utf8 -*-
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib
from smtplib import SMTP, SMTP_SSL
from email.utils import formatdate
import os

def sendMailOld(title, recipent_list,text='',html='', attache_list=[], importance = '1'):
    msg = MIMEMultipart('mixed')
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = title
    msg['From'] = 'SDTest@alipay.com'
#        msg['From'] = smtp_sender
    msg['To'] = ';'.join(recipent_list)
    msg['X-Priority'] = importance
    # Body text
    #part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html','utf-8')
    #msg.attach(part1)
    msg.attach(part2)

    for atta in attache_list:
        attach_text = MIMEText(open(atta, "rt").read())
        attach_text.add_header('Content-Disposition', 'attachment', filename=os.path.basename(atta))
        msg.attach(attach_text)
        
    server = SMTP('172.18.124.192',25)
    #server.set_debuglevel(1)
#     server.login("sdmailtest@163.com", "ali22451")
    server.sendmail('CTP@aliyun-inc.com', recipent_list, msg.as_string())

#     server = SMTP('smtp.163.com',25)
#     #server.set_debuglevel(1)
#     server.login("sdmailtest@163.com", "ali22451")
#     server.sendmail('sdmailtest@163.com', recipent_list, msg.as_string())
    server.quit()
    
def sendMail(title, recipent_list,text='',html='', attache_list=[], importance = '1'):
    msg = MIMEMultipart('mixed')
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = title
    msg['From'] = 'sdtest@alipay.com'
#        msg['From'] = smtp_sender
    msg['To'] = ';'.join(recipent_list)
    msg['X-Priority'] = importance
    # Body text
    #part1 = MIMEText(text, 'plain')
    part2 = MIMEText(html, 'html','utf-8')
    #msg.attach(part1)
    msg.attach(part2)

    for atta in attache_list:
        attach_text = MIMEText(open(atta, "rt").read())
        attach_text.add_header('Content-Disposition', 'attachment', filename=os.path.basename(atta))
        msg.attach(attach_text)
        
    try:
        s = smtplib.SMTP_SSL()
        s.connect("smtp.alibaba-inc.com")
        s.login("sdtest@alipay.com","ali22451")
        s.sendmail("sdtest@alipay.com", recipent_list, msg.as_string())
        s.close()
        return True
    except Exception, e:
        print str(e)
        return False

if __name__ == '__main__':
    #SMTPClient = SMTP(server,port)
    import sys
    print(sys.argv)
    filename = sys.argv[1]
    fd = open(filename,"rb")
    filebody = fd.read().decode("utf-8")
    fd.close()
#     mailto = sys.argv[3]
    mailto = "yixun.zengyx@alibaba-inc.com"
    recpt = mailto.split(';')
    print sendMail(title=sys.argv[2],recipent_list=recpt,html=filebody)
